package com.dualsportmaps.android.views;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Vector;

import net.osmand.LogUtil;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.views.OsmandMapLayer;
import net.osmand.plus.views.OsmandMapTileView;

import org.apache.commons.logging.Log;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import com.dualsportmaps.android.DsmResourceManager;
import com.dualsportmaps.android.DsmResourceManager.DsmResourceManagerException;
import com.dualsportmaps.android.DsmResourceManager.DsmWeatherDataDownloadRequest;
import com.dualsportmaps.android.DsmResourceManager.DsmWeatherImageDownloadRequest;

public class DsmWeatherLayer implements OsmandMapLayer {
	private static Vector<Location> radarLocations = null;
	private static Vector<String> radarNames = null;
	private OsmandMapTileView view;
	private HashMap<String,WeatherBitmap> wb;
	private Paint paintImg;
	private ArrayList<String> currentList = new ArrayList<String>();
	private DisplayMetrics dm;
	private static final Log log = LogUtil.getLog(DsmWeatherLayer.class);
	public DsmWeatherLayer() {}

	@Override
	public void initLayer(OsmandMapTileView view) {
		this.view = view;
		dm = new DisplayMetrics();
		WindowManager wmgr = (WindowManager) view.getContext().getSystemService(Context.WINDOW_SERVICE);
		wmgr.getDefaultDisplay().getMetrics(dm);
		wb = new HashMap<String,WeatherBitmap>();
		paintImg = new Paint();
		paintImg.setAlpha(150);
	}

	@Override
	public synchronized void onDraw(Canvas canvas, RectF latlonRect, boolean nightMode) {
		if(OsmandSettings.isInternetConnectionAvailable(DsmResourceManager.getApplicationContext())) {
			if(view.getZoom() > 7 && view.getZoom() < 13) { 
				if(view.getZoom() > 9)
					paintImg.setFilterBitmap(true);
				else
					paintImg.setFilterBitmap(false);
				Vector<Integer> mapstodraw = new Vector<Integer>();
				int x = 0;
				for (Location l : getRadarStations()) {
					float centerx = new Double(l.longitude).floatValue();
					float centery = new Double(l.latitude).floatValue();
					RectF latlonRectc = new RectF(latlonRect);
					latlonRectc.sort();
					RectF radarcoverage = new RectF(centerx-1.25f,centery+1.5f,centerx+1.25f,centery-1.5f);
					radarcoverage.sort();
					if (RectF.intersects(latlonRectc,radarcoverage) || latlonRectc.contains(radarcoverage) || radarcoverage.contains(latlonRectc)) {
						mapstodraw.add(x);
					}
					x++;
				}
				currentList = new ArrayList<String>();
				int xxx = 0;
				for (Integer i : mapstodraw) {
					drawmap(i,canvas,latlonRect);
					xxx++;
					if(xxx >= 3) {
						break;
					}
				}
				cleanup(5);
			} else {
				currentList = new ArrayList<String>();
				cleanup(5);
			}
		}
	}
	private synchronized void cleanup(int threshold) {
		if(wb.size() > threshold) {
			ArrayList<String> dump = new ArrayList<String>(wb.keySet());
			dump.removeAll(currentList);
			for(String wwwb : dump) {
				WeatherBitmap blah = wb.get(wwwb);
				if(blah.bitmap != null) {
					if (log.isDebugEnabled()) {
						log.debug("Clearing weather image: " + wwwb); 
					}
					blah.bitmap.recycle();
					blah.bitmap=null;
				}
			}
		}
	}
	private void drawmap(Integer i,Canvas canvas,RectF latlonRect) {
		WeatherBitmap myWB = wb.get(getRadarNames().get(i));
		if(myWB == null) {
			myWB = new WeatherBitmap(getRadarNames().get(i));
			wb.put(getRadarNames().get(i),myWB);
			try {
				DsmResourceManager.getDsmResourceManager().asyncLoader.requestToLoadWeatherData(new DsmWeatherDataDownloadRequest(myWB));
			} catch (DsmResourceManagerException e) {}
		}
		if(myWB.pixelX > 0) {
			int width = view.getWidth();
			int height = view.getHeight();
			float photowidthmultiplier = Math.abs(latlonRect.width()/width);
			float photoheightmultiplier = Math.abs(latlonRect.height()/height);
			RectF photoRect = new RectF(myWB.ulX,myWB.ulY,myWB.ulX+myWB.pixelX*myWB.bitmapWidth,myWB.ulY+myWB.pixelY*myWB.bitmapHeight);
			RectF latlonRectc = new RectF(latlonRect);
			RectF photoRectc = new RectF(photoRect);
			photoRectc.sort();
			latlonRectc.sort();
			try {
				if(RectF.intersects(photoRectc,latlonRectc) || photoRectc.contains(latlonRectc) || latlonRectc.contains(photoRectc)) {
					currentList.add(myWB.code);
					photoRectc.intersect(latlonRectc);
					//Draw Icon
					int shrinkageSize = 42 + (view.getZoom()-8)*2;	
					int[] ps = new int[] {view.getMapXForPoint(getRadarStations().get(i).longitude), view.getMapYForPoint(getRadarStations().get(i).latitude)}; 
//						view.getRotatedMapPoints(getRadarStations().get(i).latitude,getRadarStations().get(i).longitude);
					int ix = ps[0]-shrinkageSize/2;
					int iy = ps[1]-shrinkageSize/2;
					canvas.drawBitmap(Bitmap.createScaledBitmap(DsmResourceManager.getIcon("radar"),shrinkageSize,shrinkageSize,true),ix,iy,null);
					//Draw Icon
					try {
						DsmResourceManager.getDsmResourceManager().asyncLoader.requestToLoadWeatherImage(new DsmWeatherImageDownloadRequest(myWB));
					} catch (DsmResourceManagerException e) {}
					if(myWB.bitmap != null) {
						if(view.getRotate() != 0) {
							Rect srcRect = new Rect(view.getMapXForPoint(myWB.ulX),view.getMapYForPoint(myWB.ulY), view.getMapXForPoint(myWB.ulX+(myWB.pixelX*myWB.bitmapWidth)), view.getMapYForPoint(myWB.ulY+(myWB.pixelY*myWB.bitmapHeight)));
							canvas.drawBitmap(myWB.bitmap, null, srcRect, paintImg);
						} else {
							Rect srcRect = new Rect((int)(Math.abs(photoRectc.left-myWB.ulX)/Math.abs(myWB.pixelX)),(int)(Math.abs(photoRectc.bottom-myWB.ulY)/Math.abs(myWB.pixelY)), (int)(Math.abs(photoRectc.right-myWB.ulX)/Math.abs(myWB.pixelX)), (int)(Math.abs(photoRectc.top-myWB.ulY)/Math.abs(myWB.pixelY)));
							int x = view.getMapXForPoint(photoRectc.left);
							int y = view.getMapYForPoint(photoRectc.bottom);
							Rect dstRect = new Rect(x,y,x+(int)((Math.abs(photoRectc.width())/photowidthmultiplier)),y+(int)((Math.abs(photoRectc.height())/photoheightmultiplier)));
							canvas.drawBitmap(myWB.bitmap, srcRect, dstRect, paintImg);
						}
					}
				}
			} catch (Throwable e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public void destroyLayer() {
		view = null;
		currentList = new ArrayList<String>();
		cleanup(0);
		wb.clear();
	}

	@Override
	public boolean onTouchEvent(PointF point) {
		if(OsmandSettings.isInternetConnectionAvailable(DsmResourceManager.getApplicationContext())) {
			int ex = (int) point.x;
			int ey = (int) point.y;
			int radius = getRadiusPoi(view.getZoom()) * 3 / 2;
			for (final String abbr : currentList) {
				try {
					Location l = getRadarStations().get(getRadarNames().indexOf(abbr));
					int x = view.getRotatedMapXForPoint(l.latitude, l.longitude);
					int y = view.getRotatedMapYForPoint(l.latitude, l.longitude);
					if (Math.abs(x - ex) <= radius && Math.abs(y - ey) <= radius) {
						String format = "Weather Station " + abbr.toUpperCase();
						AlertDialog.Builder dialog = new AlertDialog.Builder(view.getContext());
						dialog.setIcon(new BitmapDrawable(DsmResourceManager.getIcon("radar")));
						dialog.setTitle(format);
						dialog.setNeutralButton("View Forecast", new DialogInterface.OnClickListener(){

							@Override
							public void onClick(DialogInterface dialog, int which) {
								Uri uri = Uri.parse("http://m.wund.com/cgi-bin/findweather/getForecast?brand=mobile&query="+abbr.toUpperCase());
								Intent intent = new Intent(Intent.ACTION_VIEW, uri);
								view.getContext().startActivity(intent);
							}});
						final AlertDialog d = dialog.create();
						d.show();
						new Thread(new Runnable() {

							@Override
							public void run() {
								try {
									Thread.sleep(2500);
									d.dismiss();
								} catch (InterruptedException e) {
								}
							}}).start();
						return true;
					}
				} catch (Throwable e) {
					e.printStackTrace();
				}
			}
		}
		return false;
	}

	@Override
	public boolean onLongPressEvent(PointF point) {
		return false;
	}

	@Override
	public boolean drawInScreenPixels() {
		return false;
	}

	private Vector<String> getRadarNames() {
		if (radarNames == null) {
			radarNames = new Vector<String>();
			radarNames.add("ABX");
			radarNames.add("ABR");
			radarNames.add("AKQ");
			radarNames.add("AMA");
			radarNames.add("AMX");
			radarNames.add("APX");
			radarNames.add("ARX");
			radarNames.add("ATX");
			radarNames.add("BBX");
			radarNames.add("BGM");
			radarNames.add("BHX");
			radarNames.add("BIS");
			radarNames.add("BIX");
			radarNames.add("BLX");
			radarNames.add("BMX");
			radarNames.add("BOX");
			radarNames.add("BRO");
			radarNames.add("BUF");
			radarNames.add("BYX");
			radarNames.add("CAE");
			radarNames.add("CBW");
			radarNames.add("CBX");
			radarNames.add("CCX");
			radarNames.add("CLE");
			radarNames.add("CLX");
			radarNames.add("CRI");
			radarNames.add("CRP");
			radarNames.add("CXX");
			radarNames.add("CYS");
			radarNames.add("DAX");
			radarNames.add("DDC");
			radarNames.add("DFX");
			radarNames.add("DIX");
			radarNames.add("DLH");
			radarNames.add("DMX");
			radarNames.add("DOX");
			radarNames.add("DTX");
			radarNames.add("DVN");
			radarNames.add("DYX");
			radarNames.add("EAX");
			radarNames.add("EMX");
			radarNames.add("ENX");
			radarNames.add("EOX");
			radarNames.add("EPZ");
			radarNames.add("ESX");
			radarNames.add("EVX");
			radarNames.add("EWX");
			radarNames.add("EYX");
			radarNames.add("FCX");
			radarNames.add("FDR");
			radarNames.add("FDX");
			radarNames.add("FFC");
			radarNames.add("FSD");
			radarNames.add("FSX");
			radarNames.add("FTG");
			radarNames.add("FWS");
			radarNames.add("GGW");
			radarNames.add("GJX");
			radarNames.add("GLD");
			radarNames.add("GRB");
			radarNames.add("GRK");
			radarNames.add("GRR");
			radarNames.add("GSP");
			radarNames.add("GWX");
			radarNames.add("GYX");
			radarNames.add("HDX");
			radarNames.add("HGX");
			radarNames.add("HNX");
			radarNames.add("HPX");
			radarNames.add("HTX");
			radarNames.add("ICT");
			radarNames.add("ICX");
			radarNames.add("ILN");
			radarNames.add("ILX");
			radarNames.add("IND");
			radarNames.add("INX");
			radarNames.add("IWA");
			radarNames.add("IWX");
			radarNames.add("DGX");
			radarNames.add("JAX");
			radarNames.add("JGX");
			radarNames.add("JKL");
			radarNames.add("LBB");
			radarNames.add("LCH");
			radarNames.add("LIX");
			radarNames.add("LNX");
			radarNames.add("LOT");
			radarNames.add("LRX");
			radarNames.add("LSX");
			radarNames.add("LTX");
			radarNames.add("LVX");
			radarNames.add("LWX");
			radarNames.add("LZK");
			radarNames.add("MAF");
			radarNames.add("MAX");
			radarNames.add("MBX");
			radarNames.add("MHX");
			radarNames.add("MKX");
			radarNames.add("MLB");
			radarNames.add("MOB");
			radarNames.add("MPX");
			radarNames.add("MQT");
			radarNames.add("MRX");
			radarNames.add("MSX");
			radarNames.add("MTX");
			radarNames.add("MUX");
			radarNames.add("MVX");
			radarNames.add("MXX");
			radarNames.add("NKX");
			radarNames.add("NQA");
			radarNames.add("OAX");
			radarNames.add("OHX");
			radarNames.add("OKX");
			radarNames.add("OTX");
			radarNames.add("PAH");
			radarNames.add("PBZ");
			radarNames.add("PDT");
			radarNames.add("POE");
			radarNames.add("PUX");
			radarNames.add("RAX");
			radarNames.add("RGX");
			radarNames.add("RIW");
			radarNames.add("RLX");
			radarNames.add("RMX");
			radarNames.add("RTX");
			radarNames.add("SFX");
			radarNames.add("SGF");
			radarNames.add("SHV");
			radarNames.add("SJT");
			radarNames.add("SOX");
			radarNames.add("SRX");
			radarNames.add("TBW");
			radarNames.add("TFX");
			radarNames.add("TLH");
			radarNames.add("TLX");
			radarNames.add("TWX");
			radarNames.add("TYX");
			radarNames.add("UDX");
			radarNames.add("UEX");
			radarNames.add("VAX");
			radarNames.add("VBX");
			radarNames.add("VNX");
			radarNames.add("VTX");
			radarNames.add("VWX");
			radarNames.add("YUX");
			radarNames.add("PLA");
			radarNames.add("ABC");
			radarNames.add("ACG");
			radarNames.add("AEC");
			radarNames.add("AHG");
			radarNames.add("AIH");
			radarNames.add("AKC");
			radarNames.add("APD");
			radarNames.add("HKI");
			radarNames.add("HKM");
			radarNames.add("HMO");
			radarNames.add("HWA");
			radarNames.add("JAN");
		}
		return radarNames;
	}

	private Vector<Location> getRadarStations() {
		if (radarLocations == null) {
			radarLocations = new Vector<Location>();
			radarLocations.add(new Location(-106.824, 35.15));
			radarLocations.add(new Location(-98.413, 45.45600000000001));
			radarLocations.add(new Location(-77.00700000000001, 36.984));
			radarLocations.add(new Location(-101.709, 35.233));
			radarLocations.add(new Location(-80.413, 25.611));
			radarLocations.add(new Location(-84.72, 44.907));
			radarLocations.add(new Location(-91.191, 43.823));
			radarLocations.add(new Location(-122.496, 48.194));
			radarLocations.add(new Location(-121.632, 39.496));
			radarLocations.add(new Location(-75.985, 42.20000000000001));
			radarLocations.add(new Location(-124.292, 40.498));
			radarLocations.add(new Location(-100.761, 46.771));
			radarLocations.add(new Location(-88.985, 30.524));
			radarLocations.add(new Location(-108.607, 45.854));
			radarLocations.add(new Location(-86.77, 33.172));
			radarLocations.add(new Location(-71.137, 41.956));
			radarLocations.add(new Location(-97.419, 25.916));
			radarLocations.add(new Location(-78.737, 42.949));
			radarLocations.add(new Location(-81.703, 24.598));
			radarLocations.add(new Location(-81.118, 33.949));
			radarLocations.add(new Location(-67.806, 46.039));
			radarLocations.add(new Location(-116.236, 43.491));
			radarLocations.add(new Location(-78.00400000000001, 40.923));
			radarLocations.add(new Location(-81.86, 41.413));
			radarLocations.add(new Location(-81.042, 32.656));
			radarLocations.add(new Location(-97.45999999999999, 35.238));
			radarLocations.add(new Location(-97.51100000000001, 27.784));
			radarLocations.add(new Location(-73.167, 44.511));
			radarLocations.add(new Location(-104.806, 41.152));
			radarLocations.add(new Location(-121.678, 38.501));
			radarLocations.add(new Location(-99.96899999999999, 37.761));
			radarLocations.add(new Location(-100.281, 29.273));
			radarLocations.add(new Location(-74.411, 39.947));
			radarLocations.add(new Location(-92.20999999999999, 46.837));
			radarLocations.add(new Location(-93.72300000000001, 41.731));
			radarLocations.add(new Location(-75.44, 38.826));
			radarLocations.add(new Location(-83.47199999999999, 42.7));
			radarLocations.add(new Location(-90.581, 41.612));
			radarLocations.add(new Location(-99.25400000000002, 32.538));
			radarLocations.add(new Location(-94.264, 38.81));
			radarLocations.add(new Location(-110.63, 31.894));
			radarLocations.add(new Location(-74.06399999999999, 42.586));
			radarLocations.add(new Location(-85.459, 31.461));
			radarLocations.add(new Location(-106.698, 31.87300000000001));
			radarLocations.add(new Location(-114.891, 35.701));
			radarLocations.add(new Location(-85.92100000000001, 30.564));
			radarLocations.add(new Location(-98.02800000000002, 29.704));
			radarLocations.add(new Location(-117.561, 35.098));
			radarLocations.add(new Location(-80.274, 37.024));
			radarLocations.add(new Location(-98.97600000000001, 34.362));
			radarLocations.add(new Location(-103.63, 34.635));
			radarLocations.add(new Location(-84.566, 33.364));
			radarLocations.add(new Location(-96.729, 43.588));
			radarLocations.add(new Location(-111.198, 34.574));
			radarLocations.add(new Location(-104.546, 39.787));
			radarLocations.add(new Location(-97.303, 32.573));
			radarLocations.add(new Location(-106.625, 48.206));
			radarLocations.add(new Location(-108.214, 39.062));
			radarLocations.add(new Location(-101.7, 39.367));
			radarLocations.add(new Location(-88.111, 44.498));
			radarLocations.add(new Location(-97.38300000000001, 30.722));
			radarLocations.add(new Location(-85.545, 42.89400000000001));
			radarLocations.add(new Location(-82.22, 34.883));
			radarLocations.add(new Location(-88.32899999999999, 33.897));
			radarLocations.add(new Location(-70.256, 43.891));
			radarLocations.add(new Location(-106.123, 33.076));
			radarLocations.add(new Location(-95.07899999999999, 29.472));
			radarLocations.add(new Location(-119.632, 36.314));
			radarLocations.add(new Location(-87.285, 36.737));
			radarLocations.add(new Location(-86.083, 34.931));
			radarLocations.add(new Location(-97.443, 37.655));
			radarLocations.add(new Location(-112.862, 37.591));
			radarLocations.add(new Location(-83.822, 39.42));
			radarLocations.add(new Location(-89.337, 40.151));
			radarLocations.add(new Location(-86.28, 39.708));
			radarLocations.add(new Location(-95.56500000000001, 36.175));
			radarLocations.add(new Location(-111.67, 33.289));
			radarLocations.add(new Location(-85.7, 41.359));
			radarLocations.add(new Location(-89.984, 32.28));
			radarLocations.add(new Location(-81.702, 30.485));
			radarLocations.add(new Location(-83.351, 32.675));
			radarLocations.add(new Location(-83.313, 37.591));
			radarLocations.add(new Location(-101.814, 33.654));
			radarLocations.add(new Location(-93.21600000000001, 30.125));
			radarLocations.add(new Location(-89.82599999999999, 30.337));
			radarLocations.add(new Location(-100.576, 41.958));
			radarLocations.add(new Location(-88.08499999999999, 41.605));
			radarLocations.add(new Location(-116.803, 40.74));
			radarLocations.add(new Location(-90.68300000000002, 38.699));
			radarLocations.add(new Location(-78.429, 33.989));
			radarLocations.add(new Location(-85.944, 37.975));
			radarLocations.add(new Location(-77.47799999999999, 38.975));
			radarLocations.add(new Location(-92.26200000000002, 34.836));
			radarLocations.add(new Location(-102.189, 31.943));
			radarLocations.add(new Location(-122.717, 42.08100000000001));
			radarLocations.add(new Location(-100.865, 48.39300000000001));
			radarLocations.add(new Location(-76.87600000000001,
					34.77600000000001));
			radarLocations.add(new Location(-88.55100000000002,
					42.96800000000001));
			radarLocations.add(new Location(-80.654, 28.113));
			radarLocations.add(new Location(-88.24, 30.679));
			radarLocations.add(new Location(-93.566, 44.849));
			radarLocations.add(new Location(-87.548, 46.531));
			radarLocations.add(new Location(-83.402, 36.169));
			radarLocations.add(new Location(-113.986, 47.041));
			radarLocations.add(new Location(-112.448, 41.263));
			radarLocations.add(new Location(-121.898, 37.155));
			radarLocations.add(new Location(-97.32599999999999,
					47.52800000000001));
			radarLocations.add(new Location(-85.79000000000001, 32.537));
			radarLocations.add(new Location(-117.042, 32.919));
			radarLocations.add(new Location(-89.87300000000001,
					35.34500000000001));
			radarLocations.add(new Location(-96.367, 41.32));
			radarLocations.add(new Location(-86.563, 36.247));
			radarLocations.add(new Location(-72.864, 40.866));
			radarLocations.add(new Location(-117.627, 47.68));
			radarLocations.add(new Location(-88.77200000000001, 37.068));
			radarLocations.add(new Location(-80.218, 40.532));
			radarLocations.add(new Location(-118.853, 45.691));
			radarLocations.add(new Location(-92.97600000000001, 31.156));
			radarLocations.add(new Location(-104.181, 38.459));
			radarLocations.add(new Location(-78.49, 35.666));
			radarLocations.add(new Location(-119.462, 39.754));
			radarLocations.add(new Location(-108.477, 43.066));
			radarLocations.add(new Location(-81.723, 38.311));
			radarLocations.add(new Location(-75.45800000000001, 43.468));
			radarLocations.add(new Location(-122.965, 45.71500000000001));
			radarLocations.add(new Location(-112.686, 43.106));
			radarLocations.add(new Location(-93.401, 37.235));
			radarLocations.add(new Location(-93.84100000000001, 32.451));
			radarLocations.add(new Location(-100.493, 31.371));
			radarLocations.add(new Location(-117.636, 33.818));
			radarLocations.add(new Location(-94.362, 35.291));
			radarLocations.add(new Location(-82.402, 27.706));
			radarLocations.add(new Location(-111.385, 47.46));
			radarLocations.add(new Location(-84.32899999999999, 30.398));
			radarLocations.add(new Location(-97.27800000000002, 35.333));
			radarLocations.add(new Location(-96.233, 38.997));
			radarLocations.add(new Location(-75.68000000000001, 43.756));
			radarLocations.add(new Location(-102.83, 44.125));
			radarLocations.add(new Location(-98.44199999999999, 40.321));
			radarLocations.add(new Location(-83.002, 30.89));
			radarLocations.add(new Location(-120.397, 34.838));
			radarLocations.add(new Location(-98.12800000000001, 36.741));
			radarLocations.add(new Location(-119.179, 34.412));
			radarLocations.add(new Location(-87.7166, 38.2666));
			radarLocations.add(new Location(-114.657, 32.495));
			radarLocations.add(new Location(-27.322, 38.73));
			radarLocations.add(new Location(-161.876, 60.792));
			radarLocations.add(new Location(-135.529, 56.853));
			radarLocations.add(new Location(-165.295, 64.511));
			radarLocations.add(new Location(-151.351, 60.726));
			radarLocations.add(new Location(-146.303, 59.46100000000001));
			radarLocations.add(new Location(-156.629, 58.679));
			radarLocations.add(new Location(-147.502, 65.035));
			radarLocations.add(new Location(-159.552, 21.894));
			radarLocations.add(new Location(-155.778, 20.126));
			radarLocations.add(new Location(-157.18, 21.133));
			radarLocations.add(new Location(-155.569, 19.095));
			radarLocations.add(new Location(-90.08, 32.318));
		}
		return radarLocations;
	}

	private class Location {
		double latitude;
		double longitude;

		public Location(double longitude, double latitude) {
			this.latitude = latitude;
			this.longitude = longitude;
		}
	}
	public class WeatherBitmap {
		String code;
		long bitmapctms = 0;
		float pixelX = 0;
		float pixelY = 0;
		float ulX = 0;
		float ulY = 0;
		Bitmap bitmap;
		int bitmapWidth = 600;
		int bitmapHeight = 550;
		public WeatherBitmap(String code) {
			this.code = code;
		}
		public synchronized boolean getWorldFile() {
			try {
				URL data = new URL("http://radar.weather.gov/ridge/RadarImg/NCR/"
						+ this.code + "_NCR_0.gfw");
				DataInputStream in = new DataInputStream(
						(InputStream) data.getContent());
				BufferedReader br = new BufferedReader(new InputStreamReader(in));
				String strLine;
				float pixelX = 0;
				float pixelY = 0;
				float ulX = 0;
				float ulY = 0;
				int x = 0;
				while ((strLine = br.readLine()) != null) {
					if(x==0) {
						pixelX = Float.parseFloat(strLine);
					} else if(x==3) {
						pixelY = Float.parseFloat(strLine);
					} else if(x==4) {
						ulX = Float.parseFloat(strLine);
					} else if(x==5) {
						ulY = Float.parseFloat(strLine);
					}
					x++;
				}
				in.close();
				
				this.pixelX = pixelX;
				this.pixelY = pixelY;
				this.ulX = ulX;
				this.ulY = ulY;
				wb.put(code,this);
				return true;
			} catch (Exception e) {
//				e.printStackTrace();
			}
			return false;
		}
		public synchronized boolean getImage() {
			try {
				if(System.currentTimeMillis()-600000 > this.bitmapctms || this.bitmap == null) {
					String u = "http://radar.weather.gov/ridge/RadarImg/N0R/"+this.code+"_N0R_0.gif";
					URL url = new URL(u);
					if (log.isDebugEnabled()) {
						log.debug("Downloading " + u); 
					}
					Bitmap tb = BitmapFactory.decodeStream((InputStream)url.getContent()).copy(Config.ARGB_4444, true);
					this.bitmapctms = System.currentTimeMillis();
					bitmapWidth = tb.getWidth();
					bitmapHeight = tb.getHeight();
					for(int x=0;x<bitmapWidth;x++)
					{
						for(int y=0;y<bitmapHeight;y++)
					    {
					    	int color = tb.getPixel(x, y);
							if(color != 0) {
					    		int r = Color.red(color);
					    		int g = Color.green(color);
					    		int b = Color.blue(color);
						    	if((r == g && g-b == 51) || (r==b && b-g == 51) || (r == g && r == b)) //dump the greys and tans and maroons.
						        {
						    		tb.setPixel(x, y, Color.TRANSPARENT);
						        } else if((b > g && g > r && r < 100) || (g >= b && b > 100 && g-b<20 && r<20)) {
						        	tb.setPixel(x, y, Color.TRANSPARENT); //Color.argb(50, r, g, b)
						        }
					    	}
					    }
					}
					if(this.bitmap != null) {
						this.bitmap.recycle();
						this.bitmap = null;
					}
					this.bitmap = tb;
					return true;
				}
			} catch (Throwable e) {
				e.printStackTrace();
			}
			return false;
		}
	}
	private int getRadiusPoi(int zoom){
		int r = 0;
		if(zoom < 10){
			r = 8;
		} else if(zoom <= 15){
			r = 10;
		} else if(zoom == 16){
			r = 14;
		} else if(zoom == 17){
			r = 16;
		} else {
			r = 18;
		}
		return (int) (r * dm.density);
	}
}


